package com.byit.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.async.AppApplicationSolrAsync;
import com.byit.builder.MapBuilder;
import com.byit.constant.DefaultValue;
import com.byit.domain.AppApplication;
import com.byit.domain.SysDict;
import com.byit.domain.vo.DictVo;
import com.byit.enums.PromptInfo;
import com.byit.log.ChannelType;
import com.byit.log.Log;
import com.byit.log.OperatorType;
import com.byit.msg.MessageResult;
import com.byit.service.ISysDictService;
import com.byit.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统码表 前端控制器
 * </p>
 *
 * @author wangx
 * @since 2018-12-11
 */
@RestController
@RequestMapping("/api/sys/sysDict")
public class SysDictController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysDictService sysDictService;


    /**
     * 应用列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(HttpSession session,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                       @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit) {
        Page<SysDict> pageInfo = new Page<>(page, limit);
        if (StringUtils.isBlank(keyword)) { // 数据校准
            keyword = "";
        }
        Page<JSONObject> pageData = sysDictService.selectDataPage(pageInfo, MapBuilder.newMap(1)
                .setParam("keyword", '%' + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(keyword) + '%')
                .ok());

        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, pageData);
    }


    @RequestMapping("")
    public Object selectUserStatus(@RequestParam("type") String type) {
        if (StringUtil.isEmpty(type)) {
            return MessageResult.warpper(PromptInfo.ERROR_PARAM_NOT_NULL);
        }
        List<DictVo> list = sysDictService.selectDictByType(type);
        return MessageResult.success(list);
    }

    /**
     * 获取
     *
     * @param id id
     * @return
     */
    @GetMapping("/get")
    @ResponseBody
    public Object list(@RequestParam("id") int id) {
        return MessageResult.success(sysDictService.selectById(id));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Log(title = "数据字典", operate = OperatorType.DATA_DELETE, channel = ChannelType.WEB_INTER)
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(HttpSession session, @RequestParam("ids") String ids) {
        List<Long> idList = StringUtil.splitLong(ids, ",");

        if (idList.size() == 0) {
            return MessageResult.warpper(PromptInfo.SYS_ERROR);
        }
        sysDictService.deleteBatchIds(idList);
        return MessageResult.success();

    }

    /**
     * 保存或者更新
     *
     * @return
     */
    @Log(title = "数据字典", operate = OperatorType.DATA_SAVEORUPDATE, channel = ChannelType.WEB_INTER)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(SysDict dict, HttpSession session) {
        dict.setUpdateTime(new Date());
        if (dict.getId() == 0) { // 新建
            dict.setCreateTime(new Date());
        }
        dict.setIsDisplay(1);

        sysDictService.insertOrUpdate(dict);
        //solr同步
        return MessageResult.success();
    }

    @GetMapping("/all")
    public Object all(HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        List<SysDict> sensitiveWordss = sysDictService.selectByMap(map);
        return MessageResult.success(sensitiveWordss);
    }

}
